define(['jquery','text!../template/task.html','text!../template/taskOpen.html','css!../css/task','common'],function($,tplTask,tplTaskOpen){
    let _task = {};
    let currentPage = 1;
    let currentType = 1;
    var tpls = {
        'tplTask' :_.template(tplTask),
        'tplTaskOpen' :_.template(tplTaskOpen)
    };
	let imgUrl = window.location.host;
	let hd = imgUrl.split('.njdtwise')[0]
	imgUrl = hd == 'www' ? 'http://www.njdtwise.com/images/' : 'http://dev.njdtwise.com/images/';
    _task.show = function(){
        currentPage = 1;
        $('#contant').html(tpls.tplTask());
        $('.taskButton').click(function(event) {
            $('.taskButton').removeClass('checkedButton');
            $(this).addClass('checkedButton');
            currentType = $(this).attr('name');
            currentType=='2'?$('.taskStatus').show():$('.taskStatus').hide();
            currentPage = 1;
            setTaskTable(0,5,currentType);
        });
        $('#taskSearch').click(function(event) {
            currentPage = 1;
            let str = $('#taskInput').val();
            setTaskTable(0,5,currentType,str);
        });

        setTaskTable(0,5,1);
    }

    function setTaskTable(from ,count,type,searchStr){
        let parms={};
        parms.status = type;
        if(type==2){
            parms.dealStatus=$('#taskStatus').val();
        }
        parms.searchParam = searchStr;
        $('#gridRepairTable').datagrid({
            url: '/xjrest/task/task_query?from='+from+'&count='+count,
            queryParams:parms,
            width: '100%',
            method: 'get',
            border: false,
            singleSelect: true,
            fitColumns:false,
            scrollbarSize :0,
            loadMsg: "正在查询,请耐心等待。。。。。。。",
            columns: [
                [
                    { field: 'taskTitle', title: '任务标题', width: currentType==2?"12%":"15%",align: 'center',formatter: function(value,row){
                        return `<span title="${value}">${truncate(value,10)}</span>`;
                    }},
                    { field: 'dealUserName', title: '处理人', width: "11%", align: 'center',hidden: currentType==2?false:true},
                    { field: 'beginTime', title: '任务开始时间', width: currentType==2?"11%":"14%", align: 'center',formatter: function(value,row){
                        return value.substring(0,10);
                    }},
                    { field: 'endTime', title: '任务截止时间', width: currentType==2?"11%":"14%", align: 'center',formatter: function(value,row){
                        return value.substring(0,10);
                    }},
                    { field: 'finishTime', title: '任务完成时间', width: currentType==2?"11%":"14%", align: 'center',formatter: function(value,row){
                        return value==''?'未完成':value;
                    }},
                    { field: 'status', title: '状态', width: currentType==2?"11%":"14%", align: 'center',formatter: function(value,row){
                        if(value==1){
                            return '待办'
                        }else if (value==2) {
                            if(row.dealStatus==1){
                                return '正常'
                            }else if(row.dealStatus==2){
                                return '待维修'
                            }else if(row.dealStatus==3){
                                return '器材过期'
                            }else if(row.dealStatus==4){
                                return '现场维修'
                            }
                        }else if (value==3) {
                            return '超时'
                        }
                    }},
                    { field: 'townName', title: '所属街道', width: currentType==2?"11%":"14%", align: 'center'},
                    { field: 'villageName', title: '所属社区', width: currentType==2?"11%":"15%", align: 'center' },
                    { field: 'dealUserPhone', title: '联系电话', width: "11%", align: 'center',hidden: currentType==2?false:true}
                ]
            ],
            loadFilter: function(data){
                let _data = data.ret.value;
                let totals = data.ret.total;
                if (totals == 0) {
                    $("#noRecordMessage").show();
                } else {
                    $("#noRecordMessage").hide();
                }
                if (0 == totals % 5) {
                    totalPage = parseInt(totals / 5);
                    if (totalPage == 0){
                         totalPage = 1;
                    }
                } else {
                    totalPage = parseInt(totals / 5) + 1;
                }
                $('#pagination').paginations({
                    currentPage: currentPage,
                    totalPage: totalPage,
                    callback: function(current) {
                        currentPage = current;
                        current = (current - 1) * 5;
                        setTaskTable(current,5,type,searchStr);
                    }
                });
                return _data;
            },
            onDblClickRow: function(rowIndex, rowData) {
				// console.log(rowData);
                Ajax("/xjrest/task/task_detail?taskId="+rowData.taskId,detailBack);
            }
        });
    }

    function detailBack(data){
		var info = {
			taskInfo:'',
			value:[]
		};
        if(data.ret.success){
			info.taskInfo=data.ret.taskInfo;
			Ajax('/xjrest/task/point_task_image?pointId='+data.ret.pointInfo.pointId,function(result){
				var value = result.ret.value;
				info.value=value;
				console.log(info);
				let _html = tpls.tplTaskOpen({data:info});
				$('body').append(_html);
				$('#closeInfo').click(function(event) {
					$('.taskOpenBack').remove();
				});
				
				var listimg = ``;
				for(var i=0;i<value.length;i++){
					listimg +=`<div style="height:auto;" class="imgcontent">
									<img onclick='showImg("${imgUrl+value[i].filePath}")' style="height:1.2rem;" src='`+imgUrl+value[i].filePath+`' alt="">
									<span class="uptime">上报时间:`+value[i].finishTime+`</span>
								</div>`;
				}
				$('.imgbox').html(listimg);
						
			})
        }
    }
	window.showImg = function (src){
	    // console.log(src);
		$('.showImgbox').attr('src',src);
		$('.showScaleImg').show();
	}
	window.showScaleImg = function (src){
	    $('.showScaleImg').hide();
	}
    return _task;
    
})